package com.exam.system.controller;

import com.exam.system.entity.*;
import com.exam.system.service.ExamLibraryService;
import com.exam.system.service.ExamLibraryTypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;


/**
 * 题库controller
 */
@Api(tags = "题库模块")
@RestController
@CrossOrigin
public class ExamLibraryController {
    @Autowired
    ExamLibraryService examLibraryService;



    @ApiOperation(value = "题库列表并通过分页显示")
    @ApiImplicitParam(name = "libraryPageDTO",value = "题库用于查询列表并分页的实体类",required = true)
    @PostMapping("findLibraryList")
    public List findLibraryList(PageDTO<ExamLibrary> libraryPageDTO){
        return examLibraryService.findLibraryList(libraryPageDTO);
    }

    /**
     * 根据题库名称、类型查询题库
     * @param
     * @return
     */
    @ApiOperation(value = "题库多条件查询并分页")
    @ApiImplicitParam(name = "libraryPageDTO",value = "题库用于多条件查询的并分页的实体类",required = true)
    @PostMapping("findLibraryByNameAndType")
    public List findLibraryByNameAndType(@RequestBody  PageDTO<ExamLibrary> libraryPageDTO){
         return  examLibraryService.findLibraryByNameAndType(libraryPageDTO);
    }

    /**
     * 添加题库
     * @param examLibrary
     */
    @ApiOperation(value = "添加题库")
    @ApiImplicitParam(name = "examLibrary",value ="添加的题库对象" ,required = true)
    @PostMapping("addLibrary")
    public String addLibrary(@RequestBody ExamLibrary examLibrary){
         examLibraryService.addLibrary(examLibrary);
            return "true";
    }

    /**
     * 查询所有的题库
     * @return 返回所有的题库
     */
    @RequestMapping("library/findLibraryList")
    @ApiOperation(value = "在添加试卷题目时显示所有的题库")
    public ResponseEntity<List<ExamLibrary>> findLibraryList(){
       return new ResponseEntity<>(200, "获取成功", examLibraryService.findAllLibraries());
    }

    /**
     * 修改题库
     * @param examLibrary
     * @return
     */
    @ApiOperation(value = "修改题库")
    @ApiImplicitParam(name = "examLibrary",value = "需要被修改的题库对象",required = true)
    @PostMapping("updateLibrary")
    public String updateLibrary (@RequestBody ExamLibrary examLibrary){
        examLibraryService.updateLibrary(examLibrary);
            return "true";

    }

    /**
     * 删除某个题库
     * @param id
     * @return
     */
    @ApiOperation(value = "删除单个题库")
    @ApiImplicitParam(name = "id",value = "被删除的题库对象的id",required = true)
    @GetMapping("delLibrary/{id}")
    public String delLibrary(@PathVariable Integer id){
        examLibraryService.delLibrary(id);
            return "true";
    }

    /**
     * 批量删除题库
     * @param delarr
     * @return
     */
    @ApiOperation(value = "批量删除题库")
    @ApiImplicitParam(name = "delarr",value ="批量删除的题库对象的id数组",required = true)
    @PostMapping("delAllLibrary")
    @ResponseBody
    public String delAllLibrary(@RequestBody String delarr){
        delarr = delarr.substring(1, delarr.length() - 1);
        String[] delArr = delarr.split(",");
        examLibraryService.delAllLibrary(delArr);
        return "true";
    }

    @ApiOperation(value = "根据题库id查询对应的试题列表并分页")
    @ApiImplicitParam(name = "libraryPageDTO",value ="需要查询试题列表的题库分页实体类",required = true)
    @PostMapping("findExamAnswerByExamLibraryId")
    public List findExamAnswerByExamLibraryId(@RequestBody PageDTO<ExamLibrary> libraryPageDTO){
        System.out.println(libraryPageDTO.getEnt().getExamQuestion().getType());
        List list = examLibraryService.findExamAnswerByExamLibraryId(libraryPageDTO);
        return list;
    }

    @ApiOperation(value = "启用单个题库")
    @ApiImplicitParam(name = "id",value = "被启用的题库对象的id",required = true)
    @GetMapping("startLibrary/{id}")
    public String startLibrary(@PathVariable Integer id){
        examLibraryService.startLibrary(id);
        return "true";
    }
}
